/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package id2212.homework2;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import marketrmi.AccountClientInter;
import marketrmi.Rejected;

/**
 *
 * @author yann
 */
public class MarketUi extends javax.swing.JFrame implements java.beans.Customizer {

    private AccountClientInter acc;
    private Object bean;
    private Client cl;
    private boolean connect; // boolean to know if the user is already connect

    /** Creates new customizer MarketUi */
    public MarketUi(Client cl) {
        this.cl = cl;
        initComponents();
        setVisible(true);
        this.connect = false;
        jButton3.setEnabled(connect);

        if (cl.clientname != null) {
            jTextField1.setText(cl.clientname);
            jTextField2.setText(cl.clientname);
        } else {
            jLabel3.setText("you don't have a bank account !!");
        }
    }

    public void setObject(Object bean) {
        this.bean = bean;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPasswordField2 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(410, 313));
        setName("Form"); // NOI18N

        jLabel2.setText("You are a new Client");
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField2.setText("Enter your name");
        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField2FocusGained(evt);
            }
        });

        jButton2.setText("Ok");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Ok");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jTextField1.setText("Enter your name");
        jTextField1.setToolTipText("Enter your name");
        jTextField1.setMinimumSize(new java.awt.Dimension(76, 20));
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField1MouseClicked(evt);
            }
        });
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField1FocusLost(evt);
            }
        });

        jLabel1.setText("You are already a client");
        jLabel1.setName("jLabel1"); // NOI18N

        jButton3.setText("Enter Market");
        jButton3.setEnabled(false);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Password");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Password");
        jLabel5.setName("jLabel5"); // NOI18N

        jPasswordField1.setName("jPasswordField1"); // NOI18N

        jPasswordField2.setName("jPasswordField2"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(33, 33, 33)
                .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton3)
                .add(24, 24, 24))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel5)
                    .add(jLabel4)
                    .add(jLabel1)
                    .add(jLabel2))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPasswordField2, 0, 0, Short.MAX_VALUE)
                    .add(jPasswordField1, 0, 0, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField2)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(31, 31, 31)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton1)
                    .add(jButton2))
                .add(95, 95, 95))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(54, 54, 54)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jButton1)
                    .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(30, 30, 30)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .add(12, 12, 12)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jButton2)
                    .add(jPasswordField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(19, 19, 19)
                        .add(jButton3)))
                .add(21, 21, 21))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Connect to an account that already exist
     * @param evt 
     */
    /**
     * Create a new account
     * @param evt 
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (jPasswordField2.getPassword().length >= 8) {
            try {
                acc = (AccountClientInter) cl.marketObj.createAccountClient(jTextField2.getText(), jPasswordField2.getText(), cl);
                // cl.marketObj.getAccount(jTextField2.getText(), jPasswordField2.getPassword().toString(), cl);
                this.connect = true;
                jButton3.setEnabled(connect);
            } catch (Rejected ex) {
                jLabel3.setText(""+ex);
            } catch (RemoteException ex) {
                jLabel3.setText("error while creating your market account");
            }
        } else {
            jLabel3.setText("password must contain more than 8 characters");
        }

    }//GEN-LAST:event_jButton2ActionPerformed
    /**
     * Open the Market UI
     * @param evt 
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        MarketResume marketResume = new MarketResume(cl, acc);
        marketResume.init();
        setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTextField2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField2FocusGained
        // TODO add your handling code here:
        jTextField2.setText("");
        jLabel3.setText("");
    }//GEN-LAST:event_jTextField2FocusGained

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        if (jPasswordField1.getPassword().length >= 8) {
            try {
                acc = (AccountClientInter) cl.marketObj.getAccount(jTextField1.getText(), jPasswordField1.getText(), (ClientInter) this.cl);
                jButton3.setEnabled(true);
            } catch (Rejected ex) {
                jLabel3.setText("wrong name or password: "+ex);
            } catch (RemoteException ex) {
                jLabel3.setText("Error while connecting to the account: " + ex);
            } catch (NullPointerException e) {
                jLabel3.setText("you don't have a market account !!");
            }
        } else {
            jLabel3.setText("password must contain more than 8 characters");
        }

    }//GEN-LAST:event_jButton1MouseClicked

    private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseClicked
        // TODO add your handling code here:
        jTextField1.setText("");
        jLabel3.setText("");
    }//GEN-LAST:event_jTextField1MouseClicked

    private void jTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusLost
        // TODO add your handling code here:
        jLabel3.setText("");
    }//GEN-LAST:event_jTextField1FocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JButton jButton1;
    javax.swing.JButton jButton2;
    javax.swing.JButton jButton3;
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel2;
    javax.swing.JLabel jLabel3;
    javax.swing.JLabel jLabel4;
    javax.swing.JLabel jLabel5;
    javax.swing.JPasswordField jPasswordField1;
    javax.swing.JPasswordField jPasswordField2;
    javax.swing.JTextField jTextField1;
    javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
